#include "stdafx.h"
#include "AIDDriveUtils.h"

#define IDR_SHELL_LOC_ROOTED            0x7E02
PIDLIST_ABSOLUTE AIDDriveUtils::CreateFromKnownFolder(REFKNOWNFOLDERID FolderId, DWORD dwFlags , HANDLE hToken)
{
	PIDLIST_ABSOLUTE m_p = NULL;
	if(FAILED(::SHGetKnownFolderIDList(FolderId, dwFlags, hToken, &m_p)))
		return NULL;
	return m_p;
}
LPCWSTR AIDDriveUtils::DbgGetIID(REFIID riid)
{
   static CComAutoCriticalSection s_lock;
   CComCritSecLock<CComCriticalSection> lock(s_lock);
   static CSimpleMap<GUID, CComBSTR> s_Cache;
   LPCWSTR pwstrResult = s_Cache.Lookup(riid);
   if( pwstrResult != NULL ) return pwstrResult;
   TCHAR szName[80] = { 0 };
   OLECHAR wszGUID[50] = { 0 };
   ::StringFromGUID2(riid, wszGUID, lengthof(wszGUID));
   // Attempt to find it in the interfaces section
   CRegKey key;
   DWORD dwType = 0;
   DWORD dwCount = sizeof(szName) - sizeof(TCHAR);
   key.Open(HKEY_CLASSES_ROOT, _T("Interface"), KEY_READ);
   if( szName[0] == '\0' && key.Open(key, wszGUID, KEY_READ) == NOERROR ) {
      ::RegQueryValueEx(key.m_hKey, NULL, NULL, &dwType, (LPBYTE) szName, &dwCount);
   }
   // Attempt to find it in the CLSID section
   key.Open(HKEY_CLASSES_ROOT, _T("CLSID"), KEY_READ);
   if( szName[0] == '\0' && key.Open(key, wszGUID, KEY_READ) == NOERROR ) {
      ::RegQueryValueEx(key.m_hKey, NULL, NULL, &dwType, (LPBYTE) szName, &dwCount);
   }
   // Attempt to find it in our Named GUIDs section
   key.Open(HKEY_CLASSES_ROOT, _T("Named GUIDs"), KEY_READ);
   if( szName[0] == '\0' && key.Open(key, wszGUID, KEY_READ) == NOERROR ) {
      ::RegQueryValueEx(key.m_hKey, NULL, NULL, &dwType, (LPBYTE) szName, &dwCount);
   }
   if( szName[0] == '\0' ) wcscpy_s(szName, lengthof(szName), CW2T(wszGUID));
   s_Cache.Add(riid, CComBSTR(szName));
   return s_Cache.Lookup(riid);
}

//LPCWSTR DbgGetPKEY(REFPROPERTYKEY pkey)
//{
//   static CComAutoCriticalSection s_lock;
//   CComCritSecLock<CComCriticalSection> lock(s_lock);
//   static CSimpleMap< PROPERTYKEY, CComBSTR, CSimpleMapPkeyEqualHelper<PROPERTYKEY, CComBSTR> > s_Cache;
//   LPCWSTR pwstrResult = s_Cache.Lookup(pkey);
//   if( pwstrResult != NULL ) return pwstrResult;
//   WCHAR wszName[80] = { 0 };
//   // Let Windows tell us the property name
//   CCoTaskString str;
//   if( SUCCEEDED( ::PSGetNameFromPropertyKey(pkey, &str) ) ) wcscpy_s(wszName, lengthof(wszName), str);
//   // Attempt to find it in our Named GUIDs section
//   if( wszName[0] == '\0' ) {
//      WCHAR wszGUID[70] = { 0 };
//      CRegKey key;
//      DWORD dwType = 0, dwCount = sizeof(wszGUID) - sizeof(TCHAR);
//      key.Open(HKEY_CLASSES_ROOT, _T("Named GUIDs"), KEY_READ);
//      if( key.Open(key, wszGUID, KEY_READ) == NOERROR ) ::RegQueryValueExW(key.m_hKey, NULL, NULL, &dwType, (LPBYTE) wszName, &dwCount);
//   }
//   // Just format the GUID
//   if( wszName[0] == '\0' ) ::PSStringFromPropertyKey(pkey, wszName, lengthof(wszName));
//   // Add it to cache and return
//   s_Cache.Add(pkey, CComBSTR(wszName));
//   return s_Cache.Lookup(pkey);
//}

LPCWSTR AIDDriveUtils::DbgGetPidl(PCIDLIST_ABSOLUTE pidl)
{
	CComBSTR	result;
	
	if (pidl == NULL)
		return result;
	//start running throught pidl data

	BYTE*	pPidl = (BYTE*) (pidl);
	size_t  crtPos = 0;
	
	while (pPidl != NULL)
	{
		USHORT cb = 0;
		::CopyMemory(&cb, pPidl, sizeof (USHORT));
		if (cb == 0)
			break;
		WCHAR pszBuf[256] = {0, };
		size_t size = 256 * sizeof (WCHAR);

		StringCbPrintf(pszBuf, size, L"|%d|", cb);
		result += pszBuf;
		pPidl += sizeof (USHORT);
		for (size_t i = 0; i < cb - sizeof (USHORT); ++i)
		{
			::ZeroMemory(pszBuf, size);
			StringCbPrintf(pszBuf, size, L"%02X ", *pPidl);
			result += pszBuf;
			++pPidl;
		}
	}

	return result;
}